package com.ssy.lingxi.member.merchant.utils;

import com.ssy.lingxi.member.merchant.model.bo.DataAuthBO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据权限DataAuthBO相关工具类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-04-12
 */
public class DataAuthUtil {

    /**
     * 根据 url、source去重合并
     * @param authList 要合并的列表
     * @return 合并后的结果
     */
    public static List<DataAuthBO> mergeAndDistinct(List<DataAuthBO> authList) {
        //DataAuthBO中必须使用url、source两个字段定义equals和hashCode方法
        return authList.stream().collect(Collectors.groupingBy(a -> {
            DataAuthBO authBO = new DataAuthBO();
            authBO.setUrl(a.getUrl());
            authBO.setSource(a.getSource());
            authBO.setUserIds(new ArrayList<>());
            return authBO;
        })).entrySet().stream().map(entry -> {
            entry.getKey().setUserIds(entry.getValue().stream().flatMap(v -> v.getUserIds().stream()).distinct().collect(Collectors.toList()));
            return entry.getKey();
        }).collect(Collectors.toList());
    }
}
